<template>
  <el-container class="home-container">
    <!-- 页面头部 -->
    <el-header class="">
      <div>
        <div class="iconfont xe676">&#xe676;</div>
        <span>电商后台管理系统</span>
      </div>
      <el-button type="info" @click="clearSes()">退出</el-button>
    </el-header>
    <!-- 页面主体区 -->
    <el-container>
      <!-- 页面左侧 -->
      <el-aside class="" :width="isCollapse? '64px' : '200px'">
        <div class="toggle-button" @click="toggleCollapse()">|||</div>
        <!-- 侧边栏菜单区域 -->
        <!-- 侧边栏打开一个其他关闭 unique-opened -->
        <el-menu
          background-color="#333744"
          text-color="#fff"
          active-text-color="#409EFF"
          :unique-opened="true"
          :collapse="isCollapse"
          :collapse-transition="false"
          :router="true"
          :default-active="activePath">
          <!-- 一级菜单 -->
          <el-submenu :index="item.id + ''" v-for="item in menulist" :key="item.id">
            <!-- 一级菜单的模板区域 -->
            <template slot="title">
              <!-- 图标 -->
              <i :class="iconObj[item.id]"></i>
              <!-- 文本 -->
              <span>{{item.authName}}</span>
            </template>
            <!-- 二级菜单 -->
            <el-menu-item :index="'/' + items.path" v-for="items in item.children" :key="items.id" @click="saveNavStatus('/' + items.path)">
              <template slot="title">
                <!-- 图标 -->
                <i class="el-icon-menu"></i>
                <!-- 文本 -->
                <span>{{items.authName}}</span>
              </template>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <!-- 右侧内容区 -->
      <el-main class="">
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>
<script>
export default {
  created () {
    this.getMeauList()
    this.activePath = window.sessionStorage.getItem('activePath')
  },
  data () {
    return {
      menulist: [],
      iconObj: {
        125: 'el-icon-user-solid',
        103: 'el-icon-s-goods',
        101: 'el-icon-s-help',
        102: 'el-icon-message-solid',
        145: 'el-icon-s-order'
      },
      isCollapse: false,
      activePath: ''

    }
  },
  methods: {
    // 网页退出功能
    clearSes () {
      window.sessionStorage.clear()
      this.$router.push('/login')
    },
    async getMeauList () {
      const { data: res } = await this.$http.get('menus')
      if (!res.meta.status) return this.message.error(res.meta.msg)
      this.menulist = res.data
    },
    // 保存激活状态的链接
    saveNavStatus (activePath) {
      window.sessionStorage.setItem('activePath', activePath)
      this.activePath = activePath
    },
    // 左侧菜单栏的折叠与展开
    toggleCollapse () {
      this.isCollapse = !this.isCollapse
    }
  }
}
</script>
<style lang="less" scoped>
  .el-header {
    background-color: #373D41;
    display: flex;
    justify-content: space-between;
    align-items: center;
    color: #fff;
    padding-left: 0;
    font-size: 20px;
    > div {
      display: flex;
      align-items: center;
      .xe676 {
        font-size: 40px;
        margin-left: 10px;
      }
      > span {
        margin-left: 10px;
      }
    }
  }
  .el-aside {
    background-color: #333744;
  }
  .el-main {
    background-color: #EAEDF1;
  }
  .home-container {
    height: 100%;
  }
  .el-submenu {
    margin-right: 10px;
  }
  .el-menu {
    border: 0;
  }
  .toggle-button {
    background-color: #4A5064;
    font-size: 10px;
    line-height: 24px;
    color: #fff;
    text-align: center;
    letter-spacing: 0.2em;
    cursor: pointer;
    width: 100%;
  }
</style>
